﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text; 

namespace AmazonMws.WinServices.ServiceProxy
{
    /// <summary>
    /// WCF接口代理
    /// </summary>
    public static class PictureServiceProxy
    {
        private static ChannelFactory<IPictureService> tmc_API2 = null;

        private static IPictureService conn = null;

        /// <summary>
        /// 获取服务器WCF接口代理对象
        /// </summary>
        public static IPictureService ProxyEntity
        {
            get
            {
                try
                {
                    InitProxy();
                }
                catch// (Exception ex)
                {
                    try
                    {
                        InitProxy();
                    }
                    catch (Exception ex)
                    {
                        if (null != tmc_API2 && null != tmc_API2.Endpoint && null != tmc_API2.Endpoint.Address && null != tmc_API2.Endpoint.Address.Uri)
                        {
                            //Log.Write(string.Format("联接WCF服务（{0}）出错,此地址不正确或此服务不正常。", tmc_API2.Endpoint.Address.Uri.ToString()));
                        }
                        //Log.Write(ex.Message + ex.StackTrace);
                    }
                }
                return conn;
            }
        }

        /// <summary>
        /// 是否联通
        /// </summary>
        private static bool isConnect = false;

        /// <summary>
        /// 是否联通WCF
        /// </summary>
        public static bool IsConnect
        {
            get { return PictureServiceProxy.isConnect; }
            set { PictureServiceProxy.isConnect = value; }
        }

        private static void InitProxy()
        {
            if (isConnect == false || tmc_API2 == null || tmc_API2.State != System.ServiceModel.CommunicationState.Opened)
            {
                tmc_API2 = new ChannelFactory<IPictureService>(Constant.PictureServiceClientName_API);
                BasicHttpBinding binding = tmc_API2.Endpoint.Binding as BasicHttpBinding;
                if (null != binding)
                { 
                    binding.MaxBufferSize = int.MaxValue;
                    binding.MaxBufferPoolSize = 52428800;
                    binding.MaxReceivedMessageSize = int.MaxValue;
                    binding.ReaderQuotas.MaxArrayLength = 65536;
                    binding.ReaderQuotas.MaxBytesPerRead = 10 * 1024 * 1024;
                    binding.ReaderQuotas.MaxStringContentLength = 200 * 1024 * 1024; //65536;
                    binding.ReceiveTimeout = new TimeSpan(TimeSpan.TicksPerHour * 8);//设置连接自动断开的空闲时长；
                    binding.SendTimeout = new TimeSpan(TimeSpan.TicksPerHour * 8);
                    binding.TransferMode = TransferMode.Buffered;
                }
                tmc_API2.Open();
                conn = tmc_API2.CreateChannel();
                isConnect = true;
            }
        }
    }

}
